/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */
package com.jav;

import com.jav.bean.Producto;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.M3G;
import com.jav.componentes.textfield.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.util.Resources;
import java.util.Vector;

public class ProductosForm extends Form {

    private Label lb_cod = new Label("");
    private Button bt_des = createButton();
    private Label lb_prec = new Label("");

    private Label lb_pacas = new Label("");
    private Label lb_orden = new Label("");
    private Button bt_proveedor = createButton();

    private Label estado = new Label("");
    private Vector productos = new Vector();
    private int index = 0;
    Button b_primero, b_ant, b_sig, b_ultimo;
    Button bt_buscar;
    private Image imgError;
    private TextField search = new TextField("");

    public ProductosForm() {
        super("PRODUCTOS");
        Resources res = null;
        try {


            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            if (M3G.isM3GSupported()) {
                setTransitionInAnimator(Transition3D.createStaticRotation(500, true));
                setTransitionOutAnimator(Transition3D.createStaticRotation(500, false));
            } else {
                setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 500));
                setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 500));
            }

            res = Datos.getInstance().getRes();
            imgError = res.getImage("cancel.png");
            /*BUSQUEDA*/
            bt_buscar = new Button(res.getImage("search.png"));
            if (!Datos.getInstance().isVacioPro()) {
                search.setInputModeOrder(new String[]{"123"});
                search.setUseSoftkeys(false);
                bt_buscar.getStyle().setBgColor(0x1ECF35,true);
                bt_buscar.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent arg0) {
                        if (!Datos.getInstance().isVacioPro()) {
                            Producto p = Datos.getInstance().getProductoXid(search.getText());
                            if (p != null && productos.contains(p)) {
                                int in = productos.indexOf(p);
                                if (in != -1) {
                                    index = in;
                                    imprimir(p);
                                }
                            }
                        }
                    }
                });
                bt_buscar.setAlignment(Button.CENTER);
                Container ct_buscar = new Container(new BorderLayout());
                Dimension d = bt_buscar.getPreferredSize();
                d.setWidth(Math.max(d.getWidth(), 40));
                bt_buscar.setPreferredSize(d);
                ct_buscar.addComponent(BorderLayout.CENTER, search);
                ct_buscar.addComponent(BorderLayout.EAST, bt_buscar);
                addComponent(ct_buscar);
            }

            Container largest = createPair("Cód: ", lb_cod, 30);
            int largestW = largest.getComponentAt(0).getPreferredW();
            addComponent(largest);
            addComponent(createPair("Pre: ", lb_prec, largestW));
            addComponent(bt_des);
            addComponent(createPair("Pacas: ", lb_pacas, largestW));
            addComponent(createPair("Orden: ", lb_orden, largestW));
            addComponent(bt_proveedor);

            addComponent(createPair("Est: ", estado, largestW));

            Container botonera = new Container(new BoxLayout(BoxLayout.X_AXIS));
            b_primero = createotonera("|<");
            b_primero.setAlignment(CENTER);
            b_primero.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    carPrimerObj();
                }
            });

            b_ant = createotonera("<");
            b_ant.setAlignment(CENTER);
            b_ant.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    carSigAnt(-1);
                }
            });
            b_sig = createotonera(">");
            b_sig.setAlignment(CENTER);
            b_sig.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    carSigAnt(1);
                }
            });
            b_ultimo = createotonera(">|");
            b_ultimo.setAlignment(CENTER);
            b_ultimo.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    carUltimoObj();
                }
            });



            botonera.addComponent(b_primero);
            botonera.addComponent(b_ant);
            botonera.addComponent(b_sig);
            botonera.addComponent(b_ultimo);
            addComponent(botonera);



            addCommand(new Command("Atrás", res.getImage("bck.png")) {

                public void actionPerformed(ActionEvent evt) {
                    InvMovilMidlet.getMainForm().show();
                }
            });

            search.setLeftAndRightEditingTrigger(false);
            search.setNextFocusRight(bt_buscar);
            search.setNextFocusDown(bt_des);
            bt_buscar.setNextFocusDown(bt_des);
            b_primero.setNextFocusDown(search);
            b_primero.setNextFocusUp(bt_des);
            b_ant.setNextFocusDown(search);
            b_ant.setNextFocusUp(bt_des);
            b_sig.setNextFocusDown(search);
            b_sig.setNextFocusUp(bt_des);
            b_ultimo.setNextFocusDown(search);
            b_ultimo.setNextFocusUp(bt_des);
            bt_des.setNextFocusDown(b_primero);
            bt_des.setNextFocusUp(search);
            //bt_des.getStyle().setFgSelectionColor(0x000000, true);
            
            carProductos();

        } catch (Exception e) {
            Datos.getInstance().addLogs(e.getMessage());
        } finally {
            res = null;
        }

    }

    protected Container createPair(String label, Component c, int minWidth) {
        Container pair = new Container(new BorderLayout());
        Label l = new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        l.setPreferredSize(d);
        //l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST, l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }

    protected Container createPairTA(String label, Component c, int minWidth, int minHeight) {
        Container pair = new Container(new BorderLayout());
        Label l = new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        d.setHeight(Math.max(d.getHeight(), minHeight));
        l.setPreferredSize(d);
        //l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST, l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }

    private void carPrimerObj() {
        Producto pro = null;
        this.index = 0;
        pro = (Producto) productos.firstElement();
        imprimir(pro);
    }

    private void carSigAnt(int numer) {
        int sig = this.index + numer;
        if (sig < 0 || sig > (productos.size() - 1)) {
            return;
        } else {
            Producto pro = null;
            this.index = sig;
            pro = (Producto) productos.elementAt(this.index);
            imprimir(pro);
        }
    }

    private void carUltimoObj() {
        Producto pro = null;
        this.index = productos.size() - 1;
        pro = (Producto) productos.lastElement();
        imprimir(pro);
    }

    private void imprimir(Producto pro) {
        lb_cod.setText(pro.getCodigo());
        bt_des.setText(pro.getDescripcion());
        lb_prec.setText("$ " + pro.getPrecio());
        lb_pacas.setText(Integer.toString(pro.getPacas()));
        lb_orden.setText(Integer.toString(pro.getOrden()));
        bt_proveedor.setText("Proveedor:"+Datos.getInstance().getDescripcionProveedorXid(Integer.toString(pro.getProveedor())));
        estado.setText((this.index + 1) + " de " + productos.size());
    }

    private Button createotonera(String txt) {
        Button boton = new Button(txt);
        Dimension d = boton.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), 15));
        boton.setPreferredSize(d);
        return boton;
    }

    private void carProductos() {
        productos = Datos.getInstance().getProdOrd();
        if (productos == null) {
            Command okCommand = new Command("OK");
            Dialog.show("Falló!", "Debe cargar el archivo de productos previamente",
                    okCommand, new Command[]{okCommand}, Dialog.TYPE_INFO, imgError, 0, null);
            return;
        }
        carPrimerObj();
    }

    private Button createButton() {
        Button boton = new Button("") {

            public void startTicker(long delay, boolean rightToLeft) {
                super.startTicker(100, true);
            }
        };
        boton.setAlignment(CENTER);
        return boton;
    }
}
